package com.badibadi.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;

import com.badibadi.adapter.AfterLandingMyHomePageAllAdapter_1;
import com.badibadi.adapter.Popwindows_Adapter;
import com.badibadi.fragment.ActivityMyActivityFragment0;
import com.badibadi.fragment.BaseFragment;
import com.badibadi.fragment.CalendarFragment;
import com.badibadi.fragment.ActivityMyActivityFragment0.GetList;
import com.badibadi.fragment.ActivityMyActivityFragment0.getFragmentData;
import com.badibadi.fragment.CalendarFragment.CalendarGridViewAdapter;
import com.badibadi.fragment.CalendarFragment.getAdapter;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.Area_More_Activity_Model;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 俱乐部的活动 calendar_index_btn setOnCalendarItemClistener
 * 
 * @author Administrator add club Intent
 * 
 */
public class CalendarClubActivityActivity extends BaseActivity implements
		OnClickListener {
	// private int StartYY, StartMM, StartDD;
	// private int EndYY, EndMM, EndDD;
	private String cid, c_name;
	private String club_type;
	private String is_apply;
	private TextView calendar_title;
	private Button calendar_return_btn;
	// private Bundle bun;
	private Area_Rigion_Activity_Fragment0_1 activity_Fragment0;
	private CalendarFragment calendarFragment;
	private AlertDialog builder;
	private Button record_linear_add_club, record_linear_quxiao;
	private Results resultsdd;
	private boolean is_true = true;
	// private List<Activity_Overview_Model> tongji_main_list;

	private RelativeLayout bottom_btn1, bottom_btn2;
	// /...黄色是当天，淡蓝色是点击瞬间，深蓝是有活动的天，开始一个标签，结束一个标签
	private TabHost tabhost;
	private TabHost tabhost2;
	private FragmentManager manager;
	private Bundle bun;
	// 添加开始年月日button的判断的int类型
	private int StartYY, StartMM, StartDD;
	private int EndYY, EndMM, EndDD;
	private int Judge_YY, Judge_MM, Judge_DD;
	private String d_content;
	private List<Activity_Overview_Model> models, models_1;
	// private boolean is_canyu = true;// true为发起，false为参与
	private boolean is_rili = false;// 判断是否是日历模式
	private Results results;
	private List<Activity_Overview_Model> overview_Models_starttt;
	private Calendar calendar;

	private Handler mmHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					calendar_title.setText(msg.getData().getString("tital"));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(CalendarClubActivityActivity.this);
					Utils.showMessage(CalendarClubActivityActivity.this,
							resultsdd.getRetmsg());
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					Utils.ExitPrgress(CalendarClubActivityActivity.this);
					Utils.showMessage(CalendarClubActivityActivity.this,
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 4:
				try {
					Utils.ExitPrgress(CalendarClubActivityActivity.this);
					Utils.showMessage(CalendarClubActivityActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 5:
				try {
					Utils.ExitPrgress(CalendarClubActivityActivity.this);
					is_rili = true;
					Constants.is_initiator = true;
					FragmentTransaction transaction = manager
							.beginTransaction();
					calendarFragment = CalendarFragment.getInstance();
					if (!overview_Models_starttt.isEmpty()) {
						//    
						// "数据是:" + overview_Models_starttt.toString());
						for (int i = 0; i < overview_Models_starttt.size(); i++) {// 判断是访问用户是参与者还是发起者（a_uid,uid均为活动发起者id,需判断哪个不为空）
							if (overview_Models_starttt.get(i).getA_uid() != null) {
								if (Utils.getUid(
										CalendarClubActivityActivity.this)
										.equals(overview_Models_starttt.get(i)
												.getA_uid())) {
									models.add(overview_Models_starttt.get(i));// 发起者
								} else {

								}
								models_1.add(overview_Models_starttt.get(i));// 参与者
							} else {
								if (Utils.getUid(
										CalendarClubActivityActivity.this)
										.equals(overview_Models_starttt.get(i)
												.getUid())) {
									models.add(overview_Models_starttt.get(i));// 发起者
								} else {

								}
								models_1.add(overview_Models_starttt.get(i));// 参与者
							}
						}
//						   
//								+ models_1.size());
					} else {
//						   
					}
					calendarFragment
							.setOnCalendarOnClistener(new CalendarFragment.CalendarListener() {

								@Override
								public boolean AddStartButton(int Year,
										int Month, int Day) {
									// TODO Auto-generated method stub

									return ShowStartButton(Year, Month, Day,
											StartYY, StartMM, StartDD);

								}

								@Override
								public boolean AddEndButton(int Year,
										int Month, int Day) {
									// TODO Auto-generated method stub
									return ShowEndButton(Year, Month, Day,
											EndYY, EndMM, EndDD);
								}

								@Override
								public String AddActivity(int Year, int Month,
										int Day) {
									// TODO Auto-generated method stub
									if (Constants.is_initiator) {
										//    
										// models.size());
										return ShowActivity(Year, Month, Day,
												models);
									} else {
										//    
										// models_1.size());
										return ShowActivity(Year, Month, Day,
												models_1);
									}
								}
							});

					// calendarFragment
					// .setOnCalendarItemClistener(new
					// CalendarFragment.CalendarGridViewListener() {
					//
					// @Override
					// public void ItemListener(AdapterView<?> arg0,
					// View view, int position, long arg3,
					// CalendarGridViewAdapter calendaradapter) {
					// // TODO Auto-generated method stub
					// initOnCalendarItemClistener(arg0, view,
					// position, arg3, calendaradapter);
					//
					// }
					// });

					bun = new Bundle();
					bun.putString("cid", cid);
					bun.putString("uid",
							Utils.getUid(CalendarClubActivityActivity.this));
					calendarFragment.setArguments(bun);
					transaction.replace(R.id.calendar_layout, calendarFragment);
					transaction.commit();
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.calendar);
		init();

		LoadFragment(1);
		// LoadFragment(1, tongji_main_list);
		// ChangeToThreeTab();
		// Mode_switching.setVisibility(View.GONE);
	}

	private void init() {
		try {
			cid = getIntent().getStringExtra("cid");
			c_name = getIntent().getStringExtra("c_name");
			club_type = getIntent().getStringExtra("club_type");
			is_apply = getIntent().getStringExtra("is_apply");
			calendar_title = (TextView) findViewById(R.id.calendar_title);
			calendar_title.setText(c_name
					+ getResources().getString(R.string.l_xb38));
		} catch (Exception e) {
			// TODO: handle exception
		}
		calendar = Calendar.getInstance();
		this.findViewById(R.id.calendar_index_btn).setOnClickListener(this);
		calendar_title = (TextView) findViewById(R.id.calendar_title);
		tabhost = (TabHost) findViewById(R.id.calendar_tabhost);
		tabhost2 = (TabHost) findViewById(R.id.RigionPicture_tabhost);

		bottom_btn1 = (RelativeLayout) findViewById(R.id.calender_bottom_btn1);
		bottom_btn2 = (RelativeLayout) findViewById(R.id.calender_bottom_btn2);
		bottom_btn1.setSelected(false);
		bottom_btn2.setSelected(true);
		models = new ArrayList<Activity_Overview_Model>();
		models_1 = new ArrayList<Activity_Overview_Model>();
		overview_Models_starttt = new ArrayList<Activity_Overview_Model>();
		this.findViewById(R.id.calendar_return_btn).setOnClickListener(this);
		// Keyword_Frame.setVisibility(View.GONE);
		// RelativeLayout
		// rl=(RelativeLayout)this.findViewById(R.id.calender_bottom_btn2);
		initTabHostTWO(R.string.Sponsor, R.string.Partake);
		initTabHostTHREE(R.string.Newest, R.string.Hottest, R.string.Nearby);
		ChangeToThreeTab();
	}

	/**
	 * 要重写的第一处（初始化为tabhost2）
	 * 
	 * @param id
	 * @param id2
	 */
	protected void initTabHostTWO(int id, int id2) {
		// TODO Auto-generated method stub
		manager = getSupportFragmentManager();
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.VISIBLE);
		tabhost.setup();
		tabhost.addTab(tabhost
				.newTabSpec("calendar_tabhost one")
				.setIndicator(getResources().getString(id),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		tabhost.addTab(tabhost
				.newTabSpec("calendar_tabhost two")
				.setIndicator(getResources().getString(id2),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		TabHostUtils.SetTabHost(this, tabhost);
		tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String arg0) {
				// TODO Auto-generated method stub

				switch (tabhost.getCurrentTab()) {
				case 0:
					Constants.is_initiator = true;
					calendarFragment.getCalendarAdapter(new getAdapter() {

						@Override
						public void getAdapter(CalendarGridViewAdapter adapter) {
							// TODO Auto-generated method stub
							adapter.notifyDataSetChanged();
						}
					});
					break;
				case 1:
					Constants.is_initiator = false;
					calendarFragment.getCalendarAdapter(new getAdapter() {

						@Override
						public void getAdapter(CalendarGridViewAdapter adapter) {
							// TODO Auto-generated method stub
							adapter.notifyDataSetChanged();
						}
					});
					break;
				}
			}
		});

	}

	/**
	 * 要重写的第二处重写tabhost
	 * 
	 * @param id
	 * @param id2
	 * @param id3
	 */
	public void initTabHostTHREE(int id, int id2, int id3) {
		tabhost.setVisibility(View.GONE);
		tabhost2.setVisibility(View.VISIBLE);
		tabhost2.setup();
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost one")
				.setIndicator(getResources().getString(id),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost two")
				.setIndicator(getResources().getString(id2),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));

		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost three")
				.setIndicator(getResources().getString(id3),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		tabhost2.addTab(tabhost2
				.newTabSpec("RigionPicture_tabhost four")
				.setIndicator(getResources().getString(R.string.Deaeline),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab4));
		TabHostUtils.SetTabHost(this, tabhost2);
		tabhost2.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String arg0) {
				// TODO Auto-generated method stub

				switch (tabhost2.getCurrentTab()) {
				case 0:
					initListFragment(1);
					break;
				case 1:
					initListFragment(2);
					break;
				case 2:
					initListFragment(3);
					break;
				case 3:
					initListFragment(4);
					break;
				}
			}
		});

	}

	/**
	 * 列表的Fragment
	 */
	private void initListFragment(int t) {
		// TODO Auto-generated method stub
		is_rili = false;
		FragmentTransaction transaction = manager.beginTransaction();
		activity_Fragment0 = new Area_Rigion_Activity_Fragment0_1();
		bun = new Bundle();
		bun.putInt("type", t);
		activity_Fragment0.setArguments(bun);
		transaction.replace(R.id.calendar_layout, activity_Fragment0);
		transaction.commit();
	}

	/**
	 * 根据num判断读取的是哪一个Fragment
	 * 
	 * @param num
	 *            0读取日历模式 1读取列表模式
	 */
	private void LoadFragment(int num) {
		if (num == 0) {
			models.clear();
			models_1.clear();
			initCalendarFragment();
		} else if (num == 1) {
			models.clear();
			models_1.clear();
			initListFragment(1);
		}
	}

	/**
	 * 底部日历模式的点击事件
	 * 
	 * @param view
	 */
	public void OnClick0(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn2.setSelected(false);
			LoadFragment(0);
			OnChageTabHost0();
		}
	}

	/**
	 * 底部列表的点击事件
	 * 
	 * @param view
	 */
	public void OnClick1(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn1.setSelected(false);

			LoadFragment(1);
			OnChageTabHost1();
		}
	}

	/**
	 * 必须重写
	 */
	public void OnChageTabHost0() {
		// TODO Auto-generated method stub
		ChangeToTWOTab();
	}

	/**
	 * 必须重写
	 */
	public void OnChageTabHost1() {
		// TODO Auto-generated method stub
		ChangeToThreeTab();
	}

	protected void ChangeToThreeTab() {
		// TODO Auto-generated method stub

		tabhost.setVisibility(View.GONE);
		tabhost2.setVisibility(View.VISIBLE);
	}

	protected void ChangeToTWOTab() {

		// initTabHost(id,id2);
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.VISIBLE);

	}

	/**
	 * 隐藏掉所有的tabHost
	 */
	protected void GoneAllTabHost() {
		tabhost2.setVisibility(View.GONE);
		tabhost.setVisibility(View.GONE);
	}

	private void li_activity_show(final String start_ti, final String end_ti) {
		Utils.showPrgress(CalendarClubActivityActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("start", start_ti);
				map.put("end", end_ti);
				map.put("uid", "0");
				map.put("cid", cid);
				map.put("fid", Utils.getUid(CalendarClubActivityActivity.this));
				map.put("languageType",
						com.badibadi.mytools.Dialog
								.getSystemLanguageTypegrzx(CalendarClubActivityActivity.this));
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "li_activity_show");
				System.out.println("huodong"+t_content);
				if (t_content != null) {
					//    ("返回期间的活动是:" + t_content);
					results = Utils.checkResult_NNN(getApplicationContext(),
							t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
							overview_Models_starttt = JSONUtils
									.getListByJsonString(results.getRetmsg(),
											Activity_Overview_Model.class);
							mmHandler.sendEmptyMessage(5);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						mmHandler.sendEmptyMessage(5);
					}
				} else {
					mmHandler.sendEmptyMessage(5);
				}
			}
		});
	}

	/**
	 * 要在日历中加入数据在这里改
	 */
	private void initCalendarFragment() {
		// TODO Auto-generated method stub
		try {
			li_activity_show(Utils.getFirstday_Month(
					Get___Data(Utils.GetSystemCurrentTime1(calendar)), 0),
					Utils.getLastday_Month(
							Get___Data(Utils.GetSystemCurrentTime1(calendar)),
							0));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			   
		}
	}

	/**
	 * 获取当前时间日期的Data
	 * 
	 * @param timetime
	 * @return
	 * @throws ParseException
	 */
	private Date Get___Data(String timetime) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		date = df.parse(timetime);
		return date;
	}

	protected void initOnCalendarItemClistener(AdapterView<?> arg0, View view,
			int position, long arg3, CalendarGridViewAdapter calendaradapter) {

		//    
		// Toast.makeText(CalendarClubActivityActivity.this,
		// "" + calendaradapter.getYearMonthDay(position), 0).show();
		//    
		if (backdd(Constants.temp_map,
				calendaradapter.getYearMonthDay(position)).size() > 0) {
			Intent intent = new Intent(this, Data_Me_Activity.class);
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("日期", calendaradapter.getYearMonthDay(position));
			// intent.putExtra("uid",
			// Utils.getUid(CalendarClubActivityActivity.this));
			intent.putExtra("cid", cid);
			// intent.putExtra("fid", fid);
			startActivity(intent);
		} else {

		}
		// }

	}

	/**
	 * 匹配当前时间是否有活动
	 * 
	 * @param map匹配的map
	 * @param time传入需要匹配的时间
	 */
	public List<Integer> backdd(Map<String, Object> map, String time) {
		Set<Map.Entry<String, Object>> set = map.entrySet();
		// boolean jud = false;
		List<Integer> integers = new ArrayList<Integer>();
		for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
				.hasNext();) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
					.next();
			//    (entry.getKey() + "--->" +
			// entry.getValue());
			if (Utils.getStringMillis(time) == Utils.getStringMillis(entry
					.getKey())) {
				// jud = true;
				integers = (List<Integer>) entry.getValue();
			}
		}
		Constants.temp_num = integers;
		return integers;
	}

	protected boolean ShowStartButton(int Y, int M, int D, int YY, int MM,
			int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	protected boolean ShowEndButton(int Y, int M, int D, int YY, int MM, int DD) {
		if (YY == Y && MM == M && DD == D) {
			return true;
		} else {
			return false;
		}
	}

	public String ShowActivity(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		for (int i = 0; i < models.size(); i++) {
			if (Y == BackYear(models.get(i).getStart_time())
					&& M == BackMonth(models.get(i).getStart_time())
					&& D == BackDay(models.get(i).getStart_time())) {
				return getResources().getString(R.string.l_xb40)
						+ panduan(Y, M, D, models)
						+ getResources().getString(R.string.l_xb41);
			}
		}
		return null;
	}

	/**
	 * 判断共有多少个活动
	 * 
	 * @param Y
	 * @param M
	 * @param D
	 * @param models
	 * @return
	 */
	private int panduan(int Y, int M, int D,
			List<Activity_Overview_Model> models) {
		int num = 0;
		Constants.temp_num = new ArrayList<Integer>();
		for (int i = 0; i < models.size(); i++) {
			if (Y == BackYear(models.get(i).getStart_time())
					&& M == BackMonth(models.get(i).getStart_time())
					&& D == BackDay(models.get(i).getStart_time())) {
				Constants.temp_num.add(i);
				++num;
			} else {
				//    
				// BackYear(Utils.Get_Data(models.get(i).getStart_time(),
				// "yyyy-MM-dd", "000"))
				// + " "
				// + BackMonth(Utils.Get_Data(models.get(i)
				// .getStart_time(), "yyyy-MM-dd", "000")
				// + " "
				// + BackDay(Utils.Get_Data(models.get(i)
				// .getStart_time(), "yyyy-MM-dd",
				// "000"))));
			}
		}
		Constants.temp_map.put(Y + "-" + M + "-" + D, Constants.temp_num);
		// Constants.temp_list.add(Constants.temp_num);
		return num;
	}

	// 将时间中的年份提取出来并转换为int
	public static int BackYear(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(0, 4));
		} else {
			return 0;
		}

	}

	// 将时间中的月份提取出来并转换为int
	public static int BackMonth(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(5, 7));
		} else {
			return 0;
		}
	}

	// 将时间中的日提取出来并转换为int
	public static int BackDay(String time) {
		if (time != null) {
			return Integer.valueOf(time.substring(8, time.length()));
		} else {
			return 0;
		}
	}

	// private void init() {
	// // ((TextView) (this.findViewById(R.id.calendar_title)))
	// // .setText("什么俱乐部的活动");
	//    
	// bottom_btn1 = (RelativeLayout) findViewById(R.id.calender_bottom_btn1);
	// bottom_btn2 = (RelativeLayout) findViewById(R.id.calender_bottom_btn2);
	// bottom_btn1.setSelected(false);
	// bottom_btn2.setSelected(true);
	// try {
	// cid = getIntent().getStringExtra("cid");
	// c_name = getIntent().getStringExtra("c_name");
	// club_type = getIntent().getStringExtra("club_type");
	// is_apply = getIntent().getStringExtra("is_apply");
	// calendar_title = (TextView) findViewById(R.id.calendar_title);
	// calendar_title.setText(c_name
	// + getResources().getString(R.string.l_xb38));
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	//
	// GoneAllTabHost();
	// ImageButton btn = (ImageButton) this
	// .findViewById(R.id.calendar_index_btn);
	// btn.setImageResource(R.drawable.btn_blue_white_add);
	// btn.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// // 在这里发起活动， 如果没有加入俱乐部，就提示加入俱乐部
	// // ShowToast(getApplicationContext(),
	// // "在这里发起活动， 如果没有加入俱乐部，就提示加入俱乐部");
	// if (club_type != null && !club_type.equals("3")) {
	// Intent intent = new Intent(
	// CalendarClubActivityActivity.this,
	// PostActivity1Activity.class);
	// intent.putExtra("cid", cid);
	// startActivity(intent);
	// } else {
	// // Toast.makeText(getApplicationContext(), "请加入俱乐部",
	// // 0).show();
	// builder = new Builder(CalendarClubActivityActivity.this)
	// .setView(Back_Buju()).show();
	// }
	// }
	// });
	// calendar_return_btn = (Button) findViewById(R.id.calendar_return_btn);
	// calendar_return_btn.setOnClickListener(new OnClickListener() {
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// if (activity_Fragment0 != null) {
	// activity_Fragment0.AreaActivitySet();
	// } else {
	// CalendarClubActivityActivity.this.finish();
	// }
	// }
	// });
	// tongji_main_list = new ArrayList<Activity_Overview_Model>();
	// }

	// @Override
	// public void OnClick1(View view) {
	// // TODO Auto-generated method stub
	// super.OnClick1(view);
	// is_true = true;
	// }
	//
	// @Override
	// public void OnClick0(View view) {
	// // TODO Auto-generated method stub
	// super.OnClick0(view);
	// GoneTab();
	// is_true = false;
	// }

	// private View Back_Buju() {
	// View view = null;
	// LayoutInflater inflater = LayoutInflater
	// .from(CalendarClubActivityActivity.this);
	// view = inflater.inflate(R.layout.recordlistclub_is_add_club, null);
	// record_linear_add_club = (Button) view
	// .findViewById(R.id.record_linear_add_club);
	// record_linear_quxiao = (Button) view
	// .findViewById(R.id.record_linear_quxiao);
	// TextView infos = (TextView) view.findViewById(R.id.infos);
	// infos.setText(getResources().getString(R.string.l_xb39));
	// record_linear_add_club.setOnClickListener(new OnClickListener() {
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// // Toast.makeText(getApplicationContext(), "加入", 0).show();
	// if (is_apply.equals("0")) {
	// Add_club("0", "");
	// } else {
	// Intent intent = new Intent(
	// CalendarClubActivityActivity.this,
	// AddApplyFormActivity.class);
	// intent.putExtra("pan", "set");
	// intent.putExtra("cid", cid);
	// startActivity(intent);
	// }
	// builder.dismiss();
	// }
	// });
	// record_linear_quxiao.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View arg0) {
	// // TODO Auto-generated method stub
	// // Toast.makeText(getApplicationContext(), "取消", 0).show();
	// builder.dismiss();
	// }
	// });
	// return view;
	// }
	//
	// private void Add_club(final String is_apply, final String content) {
	// Utils.showPrgress(CalendarClubActivityActivity.this);
	// MyThreadTool.fixedThreadPool.execute(new Runnable() {
	// @Override
	// public void run() {
	// Map<String, Object> map = new HashMap<String, Object>();
	// map.put("uid", Utils.getUid(CalendarClubActivityActivity.this));
	// map.put("cid", cid);
	// map.put("is_apply", is_apply);
	// if (is_apply.equals("1")) {
	// map.put("contents", content);
	// }
	// String content = Utils.sendRequest(map, Constants.BadiUrl_1
	// + "club/attendClub");
	// if (content != null) {
	// resultsdd = Utils.checkResult_NNN(getApplicationContext(),
	// content);
	//    ("加入俱乐部返回的参数是:" + content + " "
	// + resultsdd.getRetmsg());
	// if (resultsdd != null
	// && !resultsdd.getRetmsg().equals("null")) {
	// mmHandler.sendEmptyMessage(2);
	// } else {
	// mmHandler.sendEmptyMessage(3);
	// }
	// } else {
	// mmHandler.sendEmptyMessage(4);
	// }
	// }
	// });
	// }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == 4) {
			if (activity_Fragment0 != null && is_true) {
				//    
				activity_Fragment0.AreaActivitySet();
			} else {
				//    
				CalendarClubActivityActivity.this.finish();
			}
		}
		return true;
	}

	// @Override
	// protected void initCalendarFragment(List<Activity_Overview_Model> models)
	// {
	// // TODO Auto-generated method stub activity_Fragment0
	// FragmentTransaction transaction = manager.beginTransaction();
	// calendarFragment = CalendarFragment.getInstance();
	// if (tongji_main_list != null) {
	//    
	// } else {
	//    
	// }
	// // activity_Fragment0.getSList(new
	// // CalendarClubActivityActivity.getList() {
	// //
	// // @Override
	// // public void getList(List<Activity_Overview_Model> mlist) {
	// // // TODO Auto-generated method stub
	// // tongji_main_list = mlist;
	// // }
	// // });
	// calendarFragment
	// .setOnCalendarOnClistener(new CalendarFragment.CalendarListener() {
	//
	// @Override
	// public boolean AddStartButton(int Year, int Month, int Day) {
	// // TODO Auto-generated method stub
	//
	// return ShowStartButton(Year, Month, Day, StartYY,
	// StartMM, StartDD);
	//
	// }
	//
	// @Override
	// public boolean AddEndButton(int Year, int Month, int Day) {
	// // TODO Auto-generated method stub
	// return ShowEndButton(Year, Month, Day, EndYY, EndMM,
	// EndDD);
	// }
	//
	// @Override
	// public String AddActivity(int Year, int Month, int Day) {
	// // TODO Auto-generated method stub
	//
	// // if (Year == 2014 && Month == 8 && Day == 8) {
	// // return "是福建省懂了否";
	// // }
	// // if (Year == 2014 && Month == 8 && Day == 9) {
	// // return "是福建省懂了";
	// // }
	//
	// return ShowActivity(Year, Month, Day, tongji_main_list);
	// }
	// });
	// calendarFragment
	// .setOnCalendarItemClistener(new
	// CalendarFragment.CalendarGridViewListener() {
	//
	// @Override
	// public void ItemListener(AdapterView<?> arg0, View view,
	// int position, long arg3,
	// CalendarGridViewAdapter calendaradapter) {
	// // TODO Auto-generated method stub
	// initOnCalendarItemClistener(arg0, view, position, arg3,
	// calendaradapter);
	// }
	// });
	// transaction.replace(R.id.calendar_layout, calendarFragment);
	// transaction.commit();
	// }
	//
	// @Override
	// public void initTabHostTHREE(int id, int id2, int id3) {
	// // TODO Auto-generated method stub
	// super.initTabHostTHREE(id, id2, R.string.Nearby);
	// }
	//
	// @Override
	// protected void GoneAllTabHost() {
	// // TODO Auto-generated method stub
	// super.GoneAllTabHost();
	// }
	//
	// @Override
	// public void OnChageTabHost0() {
	// // TODO Auto-generated method stub
	// GoneAllTabHost();
	// }
	//
	// @Override
	// public void OnChageTabHost1() {
	// // TODO Auto-generated method stub
	// super.OnChageTabHost1();
	// }

	// @Override
	// public void initListFragment(int t, String yes) {
	// // TODO Auto-generated method stub
	// FragmentTransaction transaction = manager.beginTransaction();
	// activity_Fragment0 = new Area_Rigion_Activity_Fragment0_1();
	// // transaction = manager.beginTransaction();
	// bun = new Bundle();
	// bun.putInt("type", t);
	// activity_Fragment0.setArguments(bun);
	// transaction.replace(R.id.calendar_layout, activity_Fragment0);
	// transaction.commit();
	// }

	@SuppressLint("ValidFragment")
	private class Area_Rigion_Activity_Fragment0_1 extends BaseFragment implements
			IXListViewListener {
		private AfterLandingMyHomePageAllAdapter_1 x1Adapter;
		private XListView xListView1;
		private PopupWindow popup = null;
		private int Page;// 当前显示数据页数
		private int Type;// 显示类型
		private List<Activity_Overview_Model> activity_Overview_Models, list;
		// private int languageType = 0;
		// private MySharePreferences preferences;
		private List<ClueTypeModel> clueTypeModels;
		private Popwindows_Adapter popwindows_Adapter;
		private ListView type_list;
		private Results results;
		private boolean judge;
		private Message message;
		private Bundle bundle;
		// private List<Activity_Overview_Model> list;
		private Area_More_Activity_Model activity_Model;
		private TextView tv_search;
		private LinearLayout ll_search;
		private boolean isReturnUp = true;// 按返回键是否回退关键字
		private boolean isSouSuo1 = true;
		private boolean isSouSuo2 = true;
		private LinearLayout Keyword_Frame;
		// 查询关键字相关
		private String searchName = null;
		private String tid = "0";// 活动类型id

		private Handler mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:

					try {
						Utils.ExitPrgress(CalendarClubActivityActivity.this);
						Utils.showMessage(CalendarClubActivityActivity.this,
								getResources().getString(R.string.l_net_error));
						// xListView1.stopRefresh();
						// xListView1.stopLoadMore();
						// xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
						onLoad();
					} catch (Exception e) {
						// TODO: handle exception
					}

					break;
				case 2:
					try {
						Utils.ExitPrgress(CalendarClubActivityActivity.this);
						activity_Overview_Models.addAll(list);
						// calendarFragment.notifyAll();
						x1Adapter.notifyDataSetChanged();
						// xListView1.stopRefresh();
						// xListView1.stopLoadMore();
						// xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
						onLoad();
					} catch (Exception e) {
						// TODO: handle exception
					}

					break;
				case 3:
					try {
						Utils.ExitPrgress(CalendarClubActivityActivity.this);
						Utils.showMessage(CalendarClubActivityActivity.this,
								getResources().getString(R.string.l_xa10));
					    onLoad();
//						xListView1.stopRefresh();
//						xListView1.stopLoadMore();
//						xListView1.setRefreshTime(""
//								+ getResources().getString(R.string.ganggang));
					} catch (Exception e) {
						// TODO: handle exception
					}

					break;
				case 4:
					try {
						Utils.ExitPrgress(CalendarClubActivityActivity.this);
						// clueTypeModels.add(0, model);
						popwindows_Adapter = new Popwindows_Adapter(
								clueTypeModels,
								CalendarClubActivityActivity.this);
						type_list.setAdapter(popwindows_Adapter);
						type_list
								.setOnItemClickListener(new OnItemClickListener() {
									@Override
									public void onItemClick(
											AdapterView<?> arg0, View arg1,
											int position, long arg3) {
										tid = clueTypeModels.get(position)
												.getId();
										tv_search.setVisibility(View.VISIBLE);
										ll_search.setVisibility(View.GONE);
										tv_search.setText(clueTypeModels.get(
												position).getName());
										isSouSuo1 = false;
										Page = 1;
										activity_Overview_Models.clear();
										x1Adapter.notifyDataSetChanged();
										// activitise(
										// Type,
										// Page,
										// areaid,
										// com.badibadi.mytools.Dialog
										// .getSystemLanguageType(CalendarClubActivityActivity.this));
										club_activitise(Page);
										if (popup != null) {
											popup.dismiss();
										}
									}
								});
					} catch (Exception e) {
						// TODO: handle exception
					}

					break;
				default:
					break;
				}
			}
		};

		public void onLoad() {
			xListView1.stopRefresh();
			xListView1.stopLoadMore();
			xListView1.setRefreshTime(""
					+ getResources().getString(R.string.ganggang));
		}

		private LinearLayout acticity_overview0_btn_type;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(final LayoutInflater inflater,
				ViewGroup container, Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			View view = inflater.inflate(
					R.layout.activity_overview_importylayout, null);
			Page = 1;
			judge = true;
			tv_search = (TextView) view.findViewById(R.id.TheKeyword);
			ll_search = (LinearLayout) view.findViewById(R.id.seach_ll);
			Keyword_Frame = (LinearLayout) view
					.findViewById(R.id.Keyword_Frame);
			Keyword_Frame.setVisibility(View.GONE);
			Type = getArguments().getInt("type", 1);
			clueTypeModels = new ArrayList<ClueTypeModel>();
			activity_Overview_Models = new ArrayList<Activity_Overview_Model>();
			x1Adapter = new AfterLandingMyHomePageAllAdapter_1(
					activity_Overview_Models,
					CalendarClubActivityActivity.this, Type, 0.0, 0.0, "2");
			// preferences = new MySharePreferences(getActivity(), "language");
			// languageType = preferences.get("language", 0);
			InitMyXListView1(view);
			// activitise(Type, Page, areaid,
			// com.badibadi.mytools.Dialog
			// .getSystemLanguageType(CalendarClubActivityActivity.this));
			club_activitise(Page);
			acticity_overview0_btn_type = (LinearLayout) view
					.findViewById(R.id.fragment_club_overview0_btn_type);
			acticity_overview0_btn_type
					.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							OpenOrClosePopWindows(inflater, v);
						}
					});
			return view;
		}

		public void AreaActivitySet() {
			if (ll_search.getVisibility() == 8) {// 隐藏
				//    ("隐藏状态，变为显示");
//				   
				// searchName = null;
				ll_search.setVisibility(View.VISIBLE);
				tv_search.setVisibility(View.GONE);
				Page = 1;
				tid = "0";
				activity_Overview_Models.clear();
				x1Adapter.notifyDataSetChanged();
				club_activitise(Page);
			} else {
//				   
				CalendarClubActivityActivity.this.finish();
				overridePendingTransition(R.anim.slide_left2,
						R.anim.slide_right2);
			}
		}

		private void club_activitise(final int page) {
			MyThreadTool.fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					list = new ArrayList<Activity_Overview_Model>();
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("cid", cid);
					map.put("languageType",
							com.badibadi.mytools.Dialog
									.getSystemLanguageTypegrzx(CalendarClubActivityActivity.this));
					map.put("page", page);
					map.put("pageNum", "8");
					map.put("typeId", tid);
					map.put("type", Type);
					String actcontent = Utils.sendRequest(map,
							Constants.BadiUrl_1 + "club/activity");
					//    ("俱乐部的活动提交的数据是:" + map.toString());
//					   
					if (actcontent != null) {
						//    ("俱乐部的活动返回结果是:" + actcontent);
						results = Utils.checkResult_NNN(
								getApplicationContext(), actcontent);
//						   
						if (results != null
								&& !results.getRetmsg().equals("null")
								&& results.isRet()) {
							try {
								list = JSONUtils.getListByJsonString(
										results.getRetmsg(),
										Activity_Overview_Model.class);
								mHandler.sendEmptyMessage(2);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								   
							}
						} else {
							if (page > 1) {
								--Page;
							} else {
								Page = 1;
							}
							mHandler.sendEmptyMessage(3);
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(1);
					}
				}
			});
		}

		private void hadType() {
			Utils.showPrgress(CalendarClubActivityActivity.this);
			MyThreadTool.fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("languageType",
							com.badibadi.mytools.Dialog
									.getSystemLanguageTypegrzx(CalendarClubActivityActivity.this));
					map.put("uid",
							Utils.getUid(CalendarClubActivityActivity.this));
					map.put("type", "3");
					map.put("cid", cid);
					String acticontent = Utils.sendRequest(map,
							Constants.BadiUrl_1 + "commonality/hadType");
					System.out.println("acticontent"+acticontent);
					if (acticontent != null) {
						Results results = Utils.checkResult_NNN(
								getApplicationContext(), acticontent);
						if (results != null && results.getRetmsg() != null
								&& results.isRet()) {
//							   ("俱乐部的活动的类别结果是:" + acticontent);
							try {
								clueTypeModels = JSONUtils.getListByJsonString(
										results.getRetmsg(),
										ClueTypeModel.class);
								System.out.println("results"+results.getRetmsg());
								mHandler.sendEmptyMessage(4);
							} catch (JSONException e) {
								   
							}
						} else {
							mHandler.sendEmptyMessage(3);
						}
					} else {
						mHandler.sendEmptyMessage(1);
					}
				}
			});
		}

		/**
		 * 打开或管理悬浮对话框
		 * 
		 * @param inflater
		 * @param v
		 */
		public void OpenOrClosePopWindows(LayoutInflater inflater, View v) {
			View root = inflater.inflate(
					R.layout.fragment_club_overview_popwindows, null);
			popup = new PopupWindow(root,
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT);
			type_list = (ListView) root.findViewById(R.id.type_list);
			popup.setBackgroundDrawable(new BitmapDrawable());
			popup.setFocusable(true);
			popup.setTouchable(true);
			hadType();
			popup.showAsDropDown(v);

		}

		public void InitMyXListView1(View view) {
			xListView1 = (XListView) view
					.findViewById(R.id.after_landing_friends00_xlistView2);
			// xListView1.setDividerHeight(10);
			xListView1.setPadding(10, 10, 10, 10);
			xListView1.setXListViewListener(this);
			xListView1.setPullLoadEnable(true);
			xListView1.setPullRefreshEnable(true);
			xListView1.setAdapter(x1Adapter);
		}

		@Override
		public void onRefresh() {
			// TODO Auto-generated method stub
			Page = 1;
			if (activity_Overview_Models != null) {
				activity_Overview_Models.clear();
			}
			// activitise(Type, Page, areaid,
			// com.badibadi.mytools.Dialog
			// .getSystemLanguageType(CalendarClubActivityActivity.this));
			club_activitise(Page);
		}

		@Override
		public void onLoadMore() {
			// TODO Auto-generated method stub
			// activitise(Type, ++Page, areaid,
			// com.badibadi.mytools.Dialog
			// .getSystemLanguageType(CalendarClubActivityActivity.this));
			club_activitise(++Page);
		}

		public List<Activity_Overview_Model> getActivity_Overview_Models() {
			return activity_Overview_Models;
		}

		public void setActivity_Overview_Models(
				List<Activity_Overview_Model> activity_Overview_Models) {
			this.activity_Overview_Models = activity_Overview_Models;
		}

		public void getSList(getList get) {
			get.getList(activity_Overview_Models);
		}

	}

	public interface getList {
		void getList(List<Activity_Overview_Model> mlist);
		// List<Activity_Overview_Models> mlist
	}

	// activity_Overview_Models

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.calendar_return_btn:
			// if(getSearchIsReturnUp)
			// CalendarMyActivityActivity.this.finish();
			// 必须重写，但是里面没有东西
			if (activity_Fragment0 != null && is_true) {
				activity_Fragment0.AreaActivitySet();
			} else {
				CalendarClubActivityActivity.this.finish();
			}
			break;
		case R.id.calendar_index_btn:
			Intent intent = new Intent(CalendarClubActivityActivity.this,
					IndexActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.slide_left, R.anim.slide_right);

			break;

		default:
			break;
		}
	}

}
